package com.ifast.asset.util;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ifast.asset.domain.Assets;
import com.ifast.asset.service.impl.AssetsServiceImpl;
import com.ifast.asset.domain.AssetAndWeakDO;
import com.ifast.asset.service.impl.AssetAndWeakServiceImpl;
import lombok.SneakyThrows;

import java.io.*;


public class CommandUtil implements Runnable{

    String command;
    String weakpassUrl;
    String username;
    String password;
    Integer assetId;
    Integer port;
    String protocol;
    AssetsServiceImpl assetsServiceImpl;
    AssetAndWeakServiceImpl assetAndWeakServiceImpl;

    public CommandUtil(String protocol, String ip , Integer port, String iniUrl, String weakpassUrl,
                       Integer assetId, AssetsServiceImpl assetsServiceImpl, AssetAndWeakServiceImpl assetAndWeakServiceImpl){

        this.command = "go run weakpass.go -proto "+ protocol+" -host "+ ip +" -port "+ port +" -conf "+iniUrl ;
        this.weakpassUrl = weakpassUrl;
        this.assetId=assetId;
        this.assetsServiceImpl=assetsServiceImpl;
        this.assetAndWeakServiceImpl=assetAndWeakServiceImpl;
        this.protocol=protocol;
        this.port=port;
    }
    @SneakyThrows
    @Override
    public void run() {
        try {
            Runtime run = Runtime.getRuntime();
            File wd = new File("/bin");
            Process proc = null;
            proc = run.exec("/bin/bash", null, wd);
            if (proc != null) {
                System.out.println("**************************开始检测");
                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
                out.println("cd " + weakpassUrl);
                out.println(command);
                out.println("exit");//这个命令必须执行，否则in流不结束。
                //shell脚本中有echo或者print输出， 会导致缓冲区被用完，程序卡死! 为了避免这种情况，
                //一定要把缓冲区读一下 reader.readLine() 读出即可，如果需要输出的话可以使用StringBuilder
                //进行拼接然后输出
                //String totle="";
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = in.readLine()) != null) {
                    sb.append(line).append("\r\n");
                    //totle +=line;
                }
                proc.waitFor();
                in.close();
                out.close();
                proc.destroy();
                System.out.println("**************************"+sb.toString());
                parse(sb);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public void parse(StringBuilder sb ){

        Assets assets = assetsServiceImpl.selectById(assetId);
        //修改weak表信息
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("assetId",assetId);
        wrapper.eq("protocol",protocol);
        //删除原始数据
        //assetAndWeakServiceImpl.delete(wrapper);

        if (sb.indexOf("Success")!=-1){

            String unAndPw = sb.substring(sb.indexOf("("),sb.indexOf(")"));
            username = unAndPw.split("/")[0].replace("(","");
            password = unAndPw.split("/")[1];
            //根据assetid查询资产信息
            if (assets.getIsweak()!=1){

                assets.setIsweak(1);
                assetsServiceImpl.updateById(assets);
            }
            //插入
            AssetAndWeakDO assetAndWeakDO = new AssetAndWeakDO();
            assetAndWeakDO.setAssetId(assetId);
            assetAndWeakDO.setProtocol(protocol);
            assetAndWeakDO.setPort(port);
            assetAndWeakDO.setUsername(username);
            assetAndWeakDO.setPassword(password);
            assetAndWeakServiceImpl.insert(assetAndWeakDO);
        }
    }
}
